<?php
/**
 * Description of global
 *
 * @author Martin Grossmann <snake.aas@gmail.com>
 * @version 1.0
 * @package 
 */
class globalFunc {
  
  private static $cipher = 'blowfish';
  private static $mode = 'cfb';
  private static $key = 'Y1MCIjc4mqp9FTAgjBlLWbW2_XS';
  
   public static function encrypt($data){    
    
    $ivSize = mcrypt_get_iv_size(self::$cipher, self::$mode);
    $iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_URANDOM);
    
    $crypted = mcrypt_encrypt(self::$cipher, self::$key, $data, self::$mode, $iv);
    
    return $iv.$crypted; 
  }

  public static function decrypt($cryptedData){

    $ivSize = mcrypt_get_iv_size(self::$cipher, self::$mode);
    $iv = substr($cryptedData, 0, $ivSize);
    
    $cryptedData = substr($cryptedData, $ivSize);
    
    $decrypted = mcrypt_decrypt(self::$cipher, self::$key, $cryptedData, self::$mode, $iv);
    
    return $decrypted;
    
  }
  
   public static function hashPassword($input, $salt = null) {
    return sha1($salt.$input);
  }
  
  public static function redirect($where = null, $append = null) {
    $location = $where;
    
    if ($location === null) {     
      $location = (isset($_SERVER["HTTP_REFERER"]) && !stripos($_SERVER["HTTP_REFERER"], "controller") && !stripos($_SERVER["HTTP_REFERER"], "activate")) ? $_SERVER["HTTP_REFERER"] : WebRoot.'index.php';            
    }
    
    header("location: ".$location.$append);
  }
  
  public static function errorMessage($message) {
    echo '<div class="ui-state-error"><span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span>'.$message.'</div>';
  }


  public static function inlineStatementError($message) {
    echo '<span style="color: red">'.$message.'</span>';
  }
  
  public static function inlineStatementOk($message) {
    echo '<span style="color: green">'.$message.'</span>';
  }
  
  public static function getUrl() {
    $pageURL = 'http';
    if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
      $pageURL .= "s";
    }
    
    $pageURL .= "://";
 
    if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].WebRoot;
    } else {
      $pageURL .= $_SERVER["SERVER_NAME"].WebRoot;
    }
    return $pageURL;
  }
  
  public static function generateToken() {
    return md5(uniqid(mt_rand(), true));
  }

 
}
?>
